package cn.edu.hzw.criminalintent.utils

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Point
import java.util.*

object PictureUtils {
    /**
     * 缩放函数
     */
    fun getScaledBitmap(path:String,activity:Activity):Bitmap{
        val size = Point()
        activity.windowManager.defaultDisplay.getSize(size)
        return getScaledBitmap(path,size.x,size.y)
    }
    fun getScaledBitmap(path:String,destWidth:Int,destHeight:Int):Bitmap{

        /**
         * 读入磁盘上图像的尺寸
         */
        var options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeFile(path,options)

        val srcWidth = options.outWidth.toFloat()
        val srcHeight = options.outHeight.toFloat()

        /**
         * 算出要缩减多少
         */
        var inSampleSize = 1
        if (srcHeight>destHeight||srcWidth>destWidth){
            val heightScale = srcHeight / destHeight
            val widthScale = srcWidth / destWidth

            val sampleScale = if (heightScale>widthScale){
                heightScale
            }else{
                widthScale
            }
            inSampleSize = Math.round(sampleScale)
        }
        options = BitmapFactory.Options()
        options.inSampleSize = inSampleSize
        /**
         * 读入并创建最终位图
         */
        return BitmapFactory.decodeFile(path,options)
    }
}